package com.javatian.controller;

import cn.hutool.json.JSONObject;
import com.javatian.common.Result;
import com.javatian.service.*;
import com.javatian.vo.EcharsData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 后台首页
 */
@RestController
@RequestMapping("/echarts")
public class EchartsController {


    @Resource
    private AdminInfoService adminInfoService;

    @Resource
    private WorkerInfoService workerInfoService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private OrderGoodsRelService orderGoodsRelService;



    /**
     * 获取总数
     */
    @GetMapping("/getTotal")
    public Result<Map<String,Object>> getTotal(){
        Map<String,Object> map = new HashMap<>();
        //用户总数
        map.put("totalUser",getUserCount());
        //总销售额
        map.put("totalPrice",orderInfoService.totalPrice());
        //总销量
        map.put("totalShopping",orderGoodsRelService.totalShopping());
        return  Result.success(map);



    }

    /**
     * 获取用户总数
     */
    private Integer getUserCount(){
        Integer count =0;
        count += adminInfoService.count();
        count += workerInfoService.count();
        count += userInfoService.count();
        return count;


    }

    /**
     * 分类总销售额
     */
    @GetMapping("/get/price")
    public Result<List<EcharsData>> getPriceEcharsData(){
        List<EcharsData> list =new ArrayList<>();
        List<Map<String, Object>> typePriceList = orderInfoService.getTypePrice();
        Map<String,Double> typeMap = new HashMap<>();
        for (Map<String, Object> map:typePriceList) {
            typeMap.put((String) map.get("name"),(Double)map.get("price"));
        }
        getPieData("分类总销售额",list,typeMap);
        getBarData("分类总销售额",list,typeMap);

        return Result.success(list);
    }

    /**
     * 分类总销量
     */
    @GetMapping("/get/shopping")
    public Result<List<EcharsData>> getShoppingEcharsData(){
        List<EcharsData> list =new ArrayList<>();
        List<Map<String, Object>> typePriceList = orderInfoService.getTypeCount();
        Map<String,Double> typeMap = new HashMap<>();
        for (Map<String, Object> map:typePriceList) {
            typeMap.put((String) map.get("name"),((BigDecimal)map.get("count")).doubleValue());
        }
        getPieData("分类总销量",list,typeMap);
        getBarData("分类总销量",list,typeMap);

        return Result.success(list);
    }

    /**
     * 获取饼图数据
     */
    private void getPieData(String name,List<EcharsData> pieList,Map<String, Double> dataMap){
        EcharsData pieData = new EcharsData();
        Map<String,String> titleMap = new HashMap<>();
        titleMap.put("text",name);
        pieData.setTitle(titleMap);

        pieData.setTooltip(new HashMap<>());

        Map<String,String> legendMap = new HashMap<>();
        legendMap.put("orient","vertical");
        legendMap.put("x","left");
        legendMap.put("y","center");
        pieData.setLegend(legendMap);

        EcharsData.Series series = new EcharsData.Series();
        series.setName(name+"比例");
        series.setRadius("60%");
        series.setType("pie");

        List<Object> objects = new ArrayList<>();
        for (String key:dataMap.keySet()){
            objects.add(new JSONObject().putOpt("name",key).putOpt("value",dataMap.get(key)));
        }
        series.setData(objects);
        pieData.setSeries(Collections.singletonList(series));
        pieList.add(pieData);
    }

    /**
     * 获取柱状图数据
     */
    private void getBarData(String name,List<EcharsData> barList,Map<String, Double> dataMap){
        EcharsData barData = new EcharsData();
        Map<String,String> titleMap = new HashMap<>();
        titleMap.put("text",name);
        barData.setTitle(titleMap);

        List<Object> xAxisObjs = new ArrayList<>();
        List<Object> seriesObjs = new ArrayList<>();
        EcharsData.Series series = new EcharsData.Series();

        for (String key:dataMap.keySet()){
            xAxisObjs.add(key);
            seriesObjs.add(dataMap.get(key));
        }

        series.setType("bar");
        series.setData(seriesObjs);
        barData.setSeries(Collections.singletonList(series));

        Map<String,Object> xAxisMap = new HashMap<>();
        xAxisMap.put("data",xAxisObjs);
        barData.setxAxis(xAxisMap);

        barData.setyAxis(new HashMap());
        barList.add(barData);
    }
}
























